<?php

class Cache
{
    static protected $vars = array();

    private function __construct()
    {

    }

    static public function instance($name = 'default')
    {
        return isset(self::$vars[$name]) ? self::$vars[$name] : (self::$vars[$name] = self::factory($name));
    }

    static public function factory($name)
    {
        $config = Agi::load_config('cache.php');

        if (!isset($config[$name]))
        {
            throw new Exception("cache config for $name does not exists!");
        }
        
        $config =  $config[$name];

        $class = 'Cache_' . ucfirst($config['driver']);
        
        return new $class($config);
    }

    static public function get($id, $default = null)
    {
        return self::instance() -> get($id, $default);
    }

    static public function set($id, $data, $lifetime = null)
    {
        return self::instance() -> set($id, $data, $lifetime);
    }

    static public function remove($id)
    {
        return self::instance() -> remove($id);

    }

    static public function remove_all()
    {
        return self::instance() -> remove_all();
    }

    static public function remove_expired()
    {
        return self::instance() -> remove_expired();
    }

}
